package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.login.GuiguLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("/api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    /**
     * 搜索首页：三级分类TOP7中的专辑TOP6
     * @param category1Id
     * @return
     */
    @GuiguLogin
    @GetMapping("channel/{category1Id}")
    public Result channel(@PathVariable("category1Id")Long category1Id){
        return Result.ok(searchService.channel(category1Id));
    }


    /**
     * 根据条件搜索专辑
     * @param albumIndexQuery
     * @return
     */
    @GuiguLogin
    @PostMapping
    public Result search(@RequestBody AlbumIndexQuery albumIndexQuery){
        return Result.ok(searchService.search(albumIndexQuery));
    }

    /**
     * 补全提示词
     * @param keywords
     * @return
     */
    @GuiguLogin
    @GetMapping("/completeSuggest/{keywords}")
    public Result completeSuggest(@PathVariable("keywords") String keywords){
        return Result.ok(searchService.completeSuggest(keywords));
    }

    /**
     * 查询专辑的详细信息
     * @param albumId
     * @return
     */
    //@GuiguCache(prefix="getAlbumInfo:")
//    @GuiguLogin
    @GetMapping("/{albumId}")
    public Result getAlbumInfo(@PathVariable("albumId")Long albumId){
        return Result.ok(searchService.getAlbumInfo(albumId));
    }


    /**
     * 查询指定一级分类下前10专辑
     * @param category1Id
     * @return
     */
    @GetMapping("/findRankingList/{category1Id}/**")
    public Result findRankingList(@PathVariable("category1Id")Long category1Id,
                                          HttpServletRequest request){
        String requestURI = request.getRequestURI();
        String[] uri = requestURI.split("/");
        String item = uri[uri.length-1];
        return Result.ok(searchService.findRankingList(category1Id + "",item));
    }

}

